/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.iterator;

import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ListIterator;

/**
 * Class {@code PrintIterator} is a <em>write-only</em> iterator used to perform
 * formatted output. It is usually used to print all the elements of a list,
 * such as :
 * <blockquote><code>
 * List list = ...
 * Algorithm.copy(list.iterator(), new PrintIterator(System.out));
 * </code></blockquote>
 * <p>
 * This class does not use generic on purpose, meaning that code such as the
 * above snippet will have warnings about being unchecked. It is OK to ignore
 * such warnings.
 *
 * @param   <E>   the type of the elements to print.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.iterator.PrintIterator">Daniel Léonard</a>
 * @version $Revision$
 */
public class PrintIterator<E> implements ListIterator<E> {

//---------------------------
// Instance variables
//---------------------------

   /** The delimiter between writes. */
   private String delimiter;

   /** The writer to write to. */
   private PrintWriter writer;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor. The delimiter is the empty string {@code ""}.
    * @param   writer   the writer to write into.
    */
   @SuppressWarnings("nls")
   public PrintIterator(Writer writer) {
      this(writer, "");
      }

   /**
    * Constructor.
    * @param   writer   the writer to write into.
    * @param   delimiter   the delimiter written after each element.
    */
   @SuppressWarnings("resource")
   public PrintIterator(Writer writer, String delimiter) {
      PrintWriter pw = this.asPrintWriter(writer);
      this.setPrintWriter(pw);
      this.setDelimiter(delimiter);
      }

   /**
    * Constructor.
    * @param   out   the stream to write into.
    */
   @SuppressWarnings("nls")
   public PrintIterator(OutputStream out) {
      this(out, "");
      }

   /**
    * Constructor.
    * @param   out   the stream to write into.
    * @param   delimiter   the delimiter written after each element.
    */
   @SuppressWarnings("resource")
   public PrintIterator(OutputStream out, String delimiter) {
      PrintWriter pw = this.asPrintWriter(out);
      this.setPrintWriter(pw);
      this.setDelimiter(delimiter);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Changes the print writer.
    * @param   writer   the new print writer.
    */
   protected void setPrintWriter(PrintWriter writer) {
      this.writer = writer;
      }

   /**
    * Gets the print writer.
    * @return   the print writer.
    */
   protected PrintWriter getPrintWriter() {
      return this.writer;
      }

   /**
    * Changes the delimiter.
    * @param   delimiter   the new delimiter.
    */
   protected void setDelimiter(String delimiter) {
      this.delimiter = delimiter;
      }

   /**
    * Gets the delimiter.
    * @return   the delimiter.
    */
   protected String getDelimiter() {
      return this.delimiter;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Casts or wraps the given writer into a {@link PrintWriter}.
    * @param   writer   the writer.
    * @return   the print writer.
    */
   @SuppressWarnings("hiding")
   protected PrintWriter asPrintWriter(Writer writer) {
      PrintWriter pw = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
      return pw;
      }

   /**
    * Creates a {@link PrintWriter} with the given stream.
    * @param   out   the stream.
    * @return   the print writer.
    */
   @SuppressWarnings("hiding")
   protected PrintWriter asPrintWriter(OutputStream out) {
      Writer writer = new OutputStreamWriter(out);
      PrintWriter pw = this.asPrintWriter(writer);
      return pw;
      }

   /**
    * Gets the error message telling that the given method is unsupported.
    * @param   method   the name of the unsupported method.
    * @return   the formatted message.
    */
   protected String getUnsupportedMethodErrorMessage(String method) {
      String key = Resources.ITERATOR_UNSUPPORTED_METHOD;
      String message = Resources.getLocalizedString(key, method);
      return message;
      }

   /**
    * Gets the error message telling that the underlying list is {@code null}.
    * @return   the formatted message.
    */
   protected String getCannotAdaptErrorMessage() {
      String key = Resources.OUTPUT_ITERATOR_CANNOT_ADAPT;
      String message = Resources.getLocalizedString(key);
      return message;
      }


//---------------------------
// Implemented methods from java.util.ListIterator
//---------------------------

   /**
    * Prints the given object followed by the delimiter.
    * @param   element   the element to print.
    *
    */
   @Override
   @SuppressWarnings("resource")
   public void set(Object element) {
      String delim = this.getDelimiter();
      PrintWriter pw = this.getPrintWriter();
      pw.print(element);
      pw.print(delim);
      pw.flush();
      }

   /**
    * The iterator always have another element.
    * @return   always {@code true}.
    * @see   #next()
    */
   @Override
   public boolean hasNext() {
      boolean has_next = true;
      return has_next;
      }

   /**
    * Always return {@code null}.
    * @return   always {@code null}.
    * @see   #hasNext()
    */
   @Override
   public E next() {
   // does nothing
      return null;
      }

   /**
    * Unsupported operation.
    * @param   element   unused.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public void add(Object element) {
      String message = this.getUnsupportedMethodErrorMessage("add(Object)");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public boolean hasPrevious() {
      String message = this.getUnsupportedMethodErrorMessage("hasPrevious()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public int nextIndex() {
      String message = this.getUnsupportedMethodErrorMessage("nextIndex()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public E previous() {
      String message = this.getUnsupportedMethodErrorMessage("previous()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public int previousIndex() {
      String message = this.getUnsupportedMethodErrorMessage("previousIndex()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public void remove() {
      String message = this.getUnsupportedMethodErrorMessage("remove()");
      throw new UnsupportedOperationException(message);
      }

   }
